package com.uzai.console.service.remoteapi.impl;

import com.alibaba.fastjson.JSONObject;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.para.TbOrderRemoteApiPara;
import com.uzai.console.service.remoteapi.TbOrderRemoteApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: jianghan
 * @Date: 2019/11/27 11:23
 * @Description:
 */
@Service
public class TbOrderRemoteApiServiceImpl implements TbOrderRemoteApiService {

    private static Logger logger = LoggerFactory.getLogger(TbOrderRemoteApiServiceImpl.class);

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    /**
     * 远程调用淘宝订单下载
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbOrderRemoteApiPara
     * @return null
     */
    public void remoteDownload(TbOrderRemoteApiPara tbOrderRemoteApiPara) {
        try{
            MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
            param.add("accessToken", tbOrderRemoteApiPara.getAccessToken());
            param.add("userId", String.valueOf(tbOrderRemoteApiPara.getUserId()));
            param.add("userName", tbOrderRemoteApiPara.getUserName());
            param.add("orderScene", String.valueOf(tbOrderRemoteApiPara.getOrderScene()));
            param.add("beginTime", String.valueOf(tbOrderRemoteApiPara.getQueryStartTime()));
            param.add("endTime", String.valueOf(tbOrderRemoteApiPara.getQueryEndTime()));
            param.add("queryType", String.valueOf(tbOrderRemoteApiPara.getQueryTimeType()));
            param.add("parentOrderNo", String.valueOf(tbOrderRemoteApiPara.getParentOrderNo()));
            param.add("childOrderNo", String.valueOf(tbOrderRemoteApiPara.getChildOrderNo()));
            param.add("turnlink", String.valueOf(tbOrderRemoteApiPara.getTurnlink()));
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(param, headers);
            ResponseEntity<JSONObject> response = restTemplate.postForEntity(uzaiConsoleSysConfig.getTbOrderDownload(), request, JSONObject.class);
            if (response.getStatusCode() != HttpStatus.OK){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
            }
            JSONObject jsonObject = response.getBody();
            if (Integer.parseInt(jsonObject.get("code").toString()) != 200){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,jsonObject.get("msg").toString());
            }else {
                logger.info("download orders request succ.");
            }
        }catch (Exception e){
            logger.info("download orders request error..{}",e.getMessage());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
        }
    }

    /**
     * 远程调用淘宝维权订单下载
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbOrderRemoteApiPara
     * @return null
     */
    public void remoteDownloadRefund(TbOrderRemoteApiPara tbOrderRemoteApiPara) {
        try{
            MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
            param.add("accessToken", tbOrderRemoteApiPara.getAccessToken());
            param.add("userId", String.valueOf(tbOrderRemoteApiPara.getUserId()));
            param.add("userName", tbOrderRemoteApiPara.getUserName());
            param.add("orderScene", String.valueOf(tbOrderRemoteApiPara.getOrderScene()));
            param.add("begin_time", String.valueOf(tbOrderRemoteApiPara.getQueryStartTime()));
            param.add("end_time", String.valueOf(tbOrderRemoteApiPara.getQueryEndTime()));
            param.add("type", String.valueOf(tbOrderRemoteApiPara.getQueryTimeType()));
            param.add("turnlink", String.valueOf(tbOrderRemoteApiPara.getTurnlink()));
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(param, headers);
            ResponseEntity<JSONObject> response = restTemplate.postForEntity(uzaiConsoleSysConfig.getTbOrderRefundDownload(), request, JSONObject.class);
            if (response.getStatusCode() != HttpStatus.OK){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
            }
            JSONObject jsonObject = response.getBody();
            if (Integer.parseInt(jsonObject.get("code").toString()) != 200){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,jsonObject.get("msg").toString());
            }else {
                logger.info("download orders request succ.");
            }
        }catch (Exception e){
            logger.info("download orders request error..{}",e.getMessage());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
        }
    }

}
